package com.hnucm.c202001020216.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.hnucm.c202001020216.DataApplication;
import com.hnucm.c202001020216.R;
import com.hnucm.c202001020216.api.CommentApi;
import com.hnucm.c202001020216.databinding.ActivityShareBinding;
import com.hnucm.c202001020216.pojo.Comment;
import com.hnucm.c202001020216.pojo.Car;
import com.hnucm.c202001020216.utils.RetrofitUtils;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShareActivity extends AppCompatActivity {

    private final static String TAG="ShareActivity";
    ActivityShareBinding shareBinding;
    ImageView shareBtn;

    TextView titleTxd;
    TextView msgTxd;
    Comment.UserCommentListDTO comment=new Comment.UserCommentListDTO();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shareBinding= DataBindingUtil.setContentView(this, R.layout.activity_share);

        shareBtn=shareBinding.textView31;
        titleTxd=shareBinding.editTextTextPersonName3;
        msgTxd=shareBinding.editTextTextPersonName4;

        //获取数据
        Car.PageDTO.ListDTO keyCar= (Car.PageDTO.ListDTO) getIntent().getSerializableExtra("keyCar");

        //设置沉浸式布局
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .init();

        //返回按钮
        shareBinding.imageView8.setOnClickListener(
                (view)-> finish()
        );

        //发布按钮，将发布的内容实体提交到后台
        shareBtn.setOnClickListener(v -> {

            comment.carId= keyCar.id;
            comment.userId= DataApplication.getDataUserDTO().userId;
            comment.userName=DataApplication.getDataUserDTO().username;
            comment.commentPic=DataApplication.getDataUserDTO().userPic;
            comment.commentContent=msgTxd.getText().toString();
            comment.commentLike=0;
            comment.commentCollect=0;
            String dataBody=new Gson().toJson(comment);

            CommentApi api= RetrofitUtils.getRetrofit(DataApplication.HttpURL).create(CommentApi.class);
            RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),dataBody);
            Call<Comment> call= api.addComment(body);
            call.enqueue(new Callback<Comment>() {
                private Call<Comment> call;
                private Response<Comment> response;

                @Override
                public void onResponse(@NonNull Call<Comment> call, Response<Comment> response) {
                    Log.e(TAG, "onResponse: "+response.body());
                    finish();
                }

                @Override
                public void onFailure(Call<Comment> call, Throwable t) {

                }
            });
        });
    }

}